<?php

/**
* @package      auto_occasion
* @subpackage   annonce
* @version      1
* @author       CMS
*/


/**
* Zone affichant la partie : photos dans l'ajout d'annonce en FO
*
* @package auto_occasion
* @subpackage annonce
*/
class FoAnnonceDeposerPhotoZone extends jZone {
 
    protected $_tplname='annonce~FoAnnonceDeposerPhoto.zone';

    /**
    * Chargement des données pour affichage
    */
    protected function _prepareTpl(){
        jClasses::inc("annonce~annonceSrv");
        jClasses::inc("commun~toolsFile");
        
        $photoOccasion = $this->param('Occas_Image');   
        $idOccasion = $this->param('idOccasion');   
        $photo_princ = '';
        if($photoOccasion <>''){

            $photo =  array();
            $trouve = false;
            $imgPath = PHOTO_ANNONCE_PATH;
            //tester si le repertoire existe
            $dirPath = JELIX_APP_WWW_PATH . PATH_ANNONCE_IMG . $photoOccasion;
            if (is_dir($dirPath)){
                //lister les images dans le repertoire
                if ($dh = opendir($dirPath)) {
                    while (($file = readdir($dh)) !== false) {
                        $filename = $dirPath . '/'. $file;
                        $ext = explode(".", $file);
                        if (file_exists($filename) && isset($ext[1]) && toolsFile::is_image($ext[1])) {
                            // formation de l'objet photo
                            $images = new stdClass();
                            $images->photo_name = PATH_ANNONCE_IMG . $photoOccasion . '/' . $file;
                            $photo[] = $images;
                            $imgPath = "";
                            $trouve = true;
                        }
                    }
                    closedir($dh);
                }
            }
            if($trouve) {
                $photo_princ = $photo[0]->photo_name;
                $this->_tpl->assign('photoOccasion', $photo[0]->photo_name);
                $this->_tpl->assign('fimage', 1);
            }

        }
        $photoSrc         = annonceSrv::getPhotoOccasion($idOccasion);
       // print_r($photoSrc);die();
        if(($photo_princ == '') && (sizeof($photoSrc) > 0)){
            $photo_princ = $photoSrc[0]->photo_name;
        }
        $this->_tpl->assign('photo_princ' , $photo_princ);
        $this->_tpl->assign('photoSrc' , $photoSrc);
    }
}